<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="d" uri="http://dgg.net/jsp/jstl/core" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%--证书匹配页面--%>
<div class="body-main">
    <input type="hidden" id="layerIndex">
    <%--生产订单ID--%>
    <input type="hidden" name="scOrderId2" id="scOrderId2" value="${scOrderId}">
    <div id="titleSearchId" class="titleSearch">
        <form id="infoSearch">

            <div class="layui-inline">
                <label>证书编号/名称：</label>
                <div class="layui-input-inline">
                    <input type="text" class="input-style" name="certificateNameToMatching" placeholder="证书编号/名称"/>
                </div>
            </div>
            <div class="layui-inline">
                <label>持证人：</label>
                <div class="layui-input-inline">
                    <input type="text" class="input-style" name="personalName" placeholder="持证人"/>
                </div>
            </div>
            <div class="layui-inline">
                <label>证书类型：</label><span style="color: red;">*</span>
                <div class="layui-input-inline">
                    <select class="select-style" name="certificateType" id="certificateType"
                            onchange="loadCertificateType('150px','#certificateType','${generalType}',1,'#certificateSpecialtys',0,'#certificateLevel')">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label>证书专业：</label><span style="color: red;">*</span>
                <div class="layui-input-inline">
                    <select class="table-select" name="certificateSpecialtys" multiple="multiple" id="certificateSpecialtys" onchange="checkCertificateSpecialty('#certificateType','#certificateSpecialtys')">
                        <option value="">请先选择证书类型</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label>证书等级：</label><span style="color: red;">*</span>
                <div class="layui-input-inline">
                    <select class="select-style" name="certificateLevel" id="certificateLevel" style="width:150px">
                        <option value="">请先选择证书类型</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label>应用范围：</label>
                <div class="layui-input-inline">
                    <select class="table-select" name="applicationScopes">
                        <option value="">请选择</option>
                        <c:forEach var="p" items="${applicationScopes}">
                            <option value="${p.code}">${p.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label>类别：</label>
                <div class="layui-input-inline">
                    <select class="table-select" name="certificateCategory">
                        <option value="">请选择</option>
                        <c:forEach var="p" items="${certificateCategorys}">
                            <option value="${p.code}">${p.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label>三类证书：</label>
                <div class="layui-input-inline">
                    <select class="table-select" name="certificateToThree">
                        <option value="">请选择</option>
                        <c:forEach var="p" items="${certificateToThrees}">
                            <option value="${p.code}">${p.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label>是否购买社保：</label>
                <div class="layui-input-inline">
                    <select class="select-style" name="socialSecurity">
                        <option value="">请选择</option>
                        <c:forEach var="p" items="${socialSecurity}">
                            <option value="${p.code}" <c:if test="${!empty scope && scope == p.code}">selected</c:if>>${p.name}</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label>社保区域：</label>
                <div class="layui-input-inline">
                    <div class="bus-plugIn" id="socialSecurityArea">
                        <input type="hidden" name="socialSecurityAreaId" class="plug-in-msg"/>
                        <input type="hidden" name="socialSecurityArea" class="plug-in-code"/>
                        <input type="text" readonly placeholder="请选择" class="chose-plugIn layui-input" id="socialSecurityAreaChange" name="socialSecurityAreaChange">
                        <div class="bus-picker">
                            <div class="bus-picker-tab">
                                <a href="javascript:void(0);" class="active">请选择</a>
                            </div>
                            <div class="bus-picker-content"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-inline">
                <label>匹配数量：</label><span style="color: red;">*</span>
                <div class="layui-input-inline">
                    <select class="table-select" name="matchNumber">
                        <c:forEach var="p" items="${matchNumber}" varStatus="status">
                            <c:if test="${status.index == 0}">
                                <option value="${p}" selected="selected">${p}</option>
                            </c:if>
                            <c:if test="${status.index != 0}">
                                <option value="${p}">${p}</option>
                            </c:if>
                        </c:forEach>
                    </select>
                </div>
            </div>

            <div class="layui-inline">
                <input id="matchFlag" value="">
                <button class="layui-btn layui-btn-mini layui-btn-normal"  type="button" onclick="matchSearch()">搜索</button>
                <button class="layui-btn layui-btn-mini layui-btn-primary"  type="button" onclick="clean()">清空</button>
            </div>
        </form>
    </div>
    <div class="table-conter">
        <input type="hidden" id="cpNumber" value="${cpNumber}">
        <label class="inline yudingClass match-del">已预订证书：<span id="reservationNum">${cpNumber}</span>个</label>
        <span class="yudingClass" onclick="cancelAdvanceBaseCredentialBySc('${scOrderId}',1)">×</span>
        <input type="hidden" id="rnNumber" value="${rnNumber}">
        <label class="inline yuyueClass match-del">已预约证书：<span id="appointmentNum">${rnNumber}</span>个</label>
        <span class="yuyueClass" onclick="deleteProductReservationBySc('${scOrderId}',1)">×</span>

        <div class="fr">
            <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="certificateAppointment('${scOrderId}')">预约</a>
            <a href="javascript:void(0)" class="layui-btn layui-btn-mini" onclick="openCertificateMatch('${scOrderId}')">匹配</a>
        </div>
    </div>
    <table id="myTable" class="cell-border crm-table">
        <thead>
        <th><input type="checkbox" class="checkall"/></th>
        <th>序列号</th>
        <th>证书编号</th>
        <th>持证人</th>
        <th>证书名称</th>
        <th>证书类型</th>
        <th>证书专业</th>
        <th>证书等级</th>
        <th>证书价格（元）</th>
        <th>应用范围</th>
        <th>类别</th>
        <th>三类证书</th>
        <th>是否购买社保</th>
        <th>社保区域</th>
        <th>操作</th>
        </thead>
    </table>
</div>
<script type="text/javascript">
    var matchFlag = 0;
    var certificatetable;
    $(document).ready(function () {
        if (parseInt($('#cpNumber').val()) > 0) {
            $('.yudingClass').show();
        } else {
            $('.yudingClass').hide();
        }
        if (parseInt($('#rnNumber').val()) > 0) {
            $('.yuyueClass').show();
        } else {
            $('.yuyueClass').hide();
        }
        //初始化证书类型
        loadCertificateType('190px', '#certificateType', '${generalType}', 0);
        certificatetable = $('#myTable').dataTableExtend({
            "ajax": {
                "url": "${rootUrl}/zs/general_credential/general_certificate_info_data.do",
                "type": "post"
            },
            "fnServerParams": function (aoData) {
                aoData = $.extend(aoData, {"isAll": "3"});//按订单签单事业部地区搜索
                aoData = $.extend(aoData, {"scOrderId": '${scOrderId}'});//生产订单id
                aoData = $.extend(aoData, $('#infoSearch').serializeObject());
                aoData = $.extend(aoData, {"matchFlag": matchFlag});
                aoData = $.extend(aoData, {"scCondition": "1"});//未过期证书
                aoData.certificateSpecialtysArray = JSON.stringify(aoData.certificateSpecialtys);// 序列化 专业多选
                delete aoData.certificateSpecialtys;// 删除 专业单选
                return aoData;
            },
            "fnInitComplete": function (settings, json) {
                //datatable加载完后重定位弹窗
                dgg.relocation($('#layerIndex').val());
            },
            "scrollX": true,//是否开启横项滚动
            "fixedColumns": {//锁定列，只有开启scrollX时才生效
                "leftColumns": 3,//左侧锁定2列
                "rightColumns": 2//右侧锁定1列
            },
            "aoColumns": [
                {
                    "title": '<input type="checkbox" class="checkall"/>',
                    "sClass": "check-th",
                    "orderable": false,
                    "data": "productId",
                    "render": function (data, type, full, meta) {
                        return '<input type="checkbox"  class="checkchild"  value="' + data + '" />';
                    }
                },
                {
                    "orderable": false,
                    "render": function (data, type, full, meta) {
                        return meta.row + 1;
                    }
                },
                {
                    "orderable": false,
                    "data": "certificateNo",
                    "render": function (data, type, full, meta) {
                        return "<a href='javascript:void(0);'  class='layui-table-link' onclick='credentialDetail(\"" + data + "\",\"" + full.id + "\")'>" + data + "</a>";
                    }
                },{
                    "orderable": false,
                    "data": "personalName",
                    "render": function (data, type, full, meta) {
                        return data;
                    }
                },
                {
                    "data": "certificateName",
                    "orderable": false
                },
                {
                    "data": "certificateType",
                    "orderable": false
                },
                {
                    "data": "certificateSpecialtys",
                    "orderable": false,
                    "render": function (data, type, row, meta) {
                        data = (null != data && "" != data) ? data : "";
                        return "<div title='" + data + "' class='f-toe' style=\"max-width:100px;word-wrap:break-word\">" + data + "</div>";
                    }
                },
                {
                    "data": "certificateLevel",
                    "orderable": false,
                    "render": function (data, type, row, meta) {
                        return data;
                    }
                },
                {
                    "data": "receiptPrice",
                    "orderable": false, "render": function (data, type, row, meta) {
                        return fmtMoney(data);
                    }
                },
                {
                    "data": "applicationScope",
                    "orderable": false
                },
                {
                    "data": "certificateCategory",
                    "orderable": false
                },
                {
                    "data": "certificateToThree",
                    "orderable": false
                },
                {
                    "data": "socialSecurity",
                    "render": function (data, type, full, meta) {
                        return data == null ? '' : (data == 0 ? '是' : '否');
                    },
                    "orderable": false
                },
                {
                    "data": "socialSecurityAreaChange",
                    "render": function (data, type, full, meta) {
                        return data == null ? '' : data;
                    },
                    "orderable": false
                },
                {
                    "orderable": false,
                    "data": {},
                    "render": function (data, type, row, meta) {
                        var operation = '<a class="layui-table-link" href="javascript:void(0)" onclick="advanceCertificate(\'' + row.id + '\',\'' + row.certificateNo + '\')">预定</a>';
                        return operation;
                    }
                }
            ]
        });

    });

    // 清空搜索
    function clean() {
        var $s = $('#titleSearchId');
        $s.find('input').val('');
        $s.find('select').val('');
        $s.find('select').val(null).trigger("change");
        $s.find('input').attr("data-id", '');
    }

    //搜索
    function matchSearch() {
        var certificateType = $("#certificateType").val();
        if (checkIsEmpty(certificateType)) {
            DGG.Warning("请选择证书类型!");
            return;
        }
        var certificateSpecialtys = $("#certificateSpecialtys").val();
        if (checkIsEmpty(certificateSpecialtys)) {
            DGG.Warning("请选择证书专业!");
            return;
        }
        var certificateLevel = $("#certificateLevel").val();
        if (checkIsEmpty(certificateLevel)) {
            DGG.Warning("请选择证书等级!");
            return;
        }
        var params = $("#infoSearch").serializeObject();
        var isNotNull = 0;
        $.each(params, function (i, item) {
            if (!checkIsEmpty(item)) {
                isNotNull = isNotNull + 1;
                return false;
            }
        });
        if (isNotNull === 0) {
            DGG.Errors("请选择搜索条件后进行查询!");
            return false;
        }
        if (params.matchNumber === '' ) {
            DGG.Errors("请选择数量!");
            return false;
        }
        matchFlag = 1;//把参数改了
        certificatetable.ajax.reload();
        setTimeout(function () {
            dgg.relocation($('#layerIndex').val());
        }, 300)
    }

    //预定证书
    function advanceCertificate(certificateId,certificateNo) {
        var scOrderId = $("#scOrderId2").val();//生产订单id
        DGG.Confire("确定预定此证书吗？", '预定证书', function () {
            DGG.loading();
            $.ajax({
                type: 'POST',
                data: {certificateId: certificateId,certificateNo:certificateNo, scOrderId: scOrderId, type: 1},//type 1是证书,2是资质
                url: sysInfo.basePath + '/zzsc/inprocess/save_certificate_advance.do',
                dataType: 'JSON',
                success: function (data) {
                    DGG.loading('close');
                    if (data.code != 0) {
                        DGG.Errors(data.msg);
                    } else {
                        DGG.Succeed("操作成功!");
                        certificatetable.ajax.reload();
                        $.ajax({//异步获取已预定证书数量
                            type: 'POST',
                            data: {scOrderId: scOrderId, type: 1},//type 1是证书,2是资质
                            url: sysInfo.basePath + '/zzsc/inprocess/query_advance_match_number.do',
                            dataType: 'JSON',
                            success: function (data) {
                                if (data.code != 0) {
                                    $('.yudingClass').hide();
                                    $('.yuyueClass').hide();
                                } else {
                                    var cpNumber = data.data.cpNumber;
                                    if (parseInt(cpNumber) > 0) {//预定
                                        $('.yudingClass').show();
                                        $('#reservationNum').text(cpNumber);
                                    } else {
                                        $('.yudingClass').hide();
                                    }
                                    var rnNumber = data.data.rnNumber;
                                    if (parseInt(rnNumber) > 0) {
                                        $('.yuyueClass').show();
                                        $('#appointmentNum').text(rnNumber);
                                    } else {
                                        $('.yuyueClass').hide();
                                    }
                                }
                            }
                        });
                    }
                },
                error: function () {
                }
            });
        });
    }

    //异步加载社保区域
    Plug.plug_ins(
        "get",
        "${cmsBaseUrl}/api/cms/tree_book/v1/get_tree_book_list.do?code=2147483647&type=2&level=1",
        "#socialSecurityArea",
        '请选择地区',
        function () {
        },
        true, '2147483647'
    );

</script>

